home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS13.ADF / FutureSound / example < prev    next >
Text File  |  1986-08-05  |  6KB  |  225 lines

  1.  
  2. ' Example of Loading and Playing Sounds with the FutureSound
  3.  
  4. ' Copyright 1986 by Applied Visions
  5.  
  6. ' Applied Visions
  7. ' 15 Oak Ridge Road
  8. ' Medford, Mass. 02155
  9. ' (617) 488-3602
  10.  
  11. ' Program and text by John Foust, 22-Jun-86
  12.  
  13.  
  14. ' ATTENTION:
  15. ' Please note the files 'future.library', 'future.bmap' and 'exec.bmap'
  16. ' must be present for this program to work.
  17.  
  18.  
  19. ' We only want to declare these functions once.
  20.  
  21. IF FuncDecl = 0 THEN
  22.   DECLARE FUNCTION FSGetSize& LIBRARY
  23.   DECLARE FUNCTION FSLoadSound& LIBRARY
  24.   DECLARE FUNCTION FSPlaySound& LIBRARY
  25.   DECLARE FUNCTION FSStopSound& LIBRARY
  26.   DECLARE FUNCTION AllocMem& LIBRARY
  27.   FuncDecl = 1
  28. END IF
  29.  
  30.  
  31. ' This tells Amiga Basic we want to use these libraries.
  32. ' It will look for them in this directory and the LIBS:
  33. ' directory, which is usually on your Workbench disk.
  34.  
  35. ' The FutureSound library
  36. LIBRARY "future.library"
  37.  
  38. ' The 'exec' library for AmigaDOS functions
  39. LIBRARY "exec.library"
  40.  
  41. ' Get a sound file name from the user.
  42.  
  43. FileEntry:
  44. PRINT
  45. PRINT "Enter the full name of a sound file > ";
  46. INPUT SoundName$
  47.  
  48. PRINT
  49. PRINT "Looking for ";SoundName$
  50. PRINT
  51.  
  52. ' Add the character number ZERO to the end of the string,
  53. ' since all C strings are terminated with a zero character,
  54. ' and the library contains C functions.
  55. SoundName$ = SoundName$+CHR$(0)
  56.  
  57. 'Call the 'future.library' FSGetSize& function to get the size of
  58. 'a given sound file.  The address of the zero-terminated string 
  59. 'is sent to the function, using the Amiga Basic SADD() function.
  60.  
  61. 'If the FSGetSize& function returns zero, then it could not open
  62. 'that file for some reason.
  63. 'Chances are, they typed the name incorrectly or that file is 
  64. 'not on this disk.
  65.  
  66. SoundSize& = FSGetSize&(SADD(SoundName$))
  67.  
  68.  
  69. IF SoundSize& = 0 THEN
  70.   PRINT "That file is not present, please try another."
  71.   GOTO FileEntry
  72. END IF
  73.  
  74. PRINT "The size of that file is"; SoundSize&; "bytes."
  75.  
  76.  
  77. 'The sound file exists, so try to load it into memory.
  78. 'We must ask the operating system for memory that can be used
  79. 'by the Amiga's sound chips.  We ask for this memory to be cleared
  80. 'to all zeroes, too.
  81. 'If there is not enough memory available in this computer
  82. 'to load this sound, this function returns zero, so warn the user.
  83.  
  84. 'If we already have some memory, then free it before asking for more.
  85. 'The MemTry& would be non-zero if there was memory allocated.
  86. IF MemTry& <> 0 THEN 
  87.   GOSUB FreeUpMem
  88. END IF
  89.  
  90. 'This number, 65537, tells AmigaDOS what type of memory to allocate.
  91. 'This is MEMF_CHIP plus MEMF_CLEAR, if you know what those are.
  92. MemType& = 65537&
  93. MemTry& = AllocMem&(SoundSize&,MemType&)
  94.  
  95. 'Reset the memory size indicator
  96. MemSize& = 0
  97.  
  98. IF MemTry& = 0 THEN
  99.   PRINT "There is not enough memory to load that sound."
  100.   'Tell them how big the sound could be, using another
  101.   'exec.library' function.
  102.   MaxSize& = AvailMem&(MemType&)
  103.   PRINT "The largest sound you can load now is";
  104.   PRINT MaxSize&; "bytes in size."
  105.   PRINT "Please try again."
  106.   GOTO FileEntry
  107. END IF
  108.  
  109. 'Since the AllocMem() succeeeded, remember how much memory to free later.
  110. MemSize& = SoundSize&
  111.  
  112. 'We have allocated a large section of memory.
  113. 'Now ask the 'future.library' to load the sound.
  114. 'If this function returns zero, then something went wrong.
  115. 'If the sound loads correctly, it returns the recording rate,
  116. 'in hertz.
  117. RecRate& = FSLoadSound&(SADD(SoundName$),MemTry&)
  118.  
  119. PRINT "The sound was recorded at"; RecRate&; "hertz."
  120.  
  121. IF RecRate& = 0 THEN
  122.   PRINT "Something went wrong in loading this sound."
  123.   PRINT "Please try again."
  124.   GOSUB FreeUpMem
  125.   GOTO FileEntry
  126. END IF
  127.  
  128. 'We have loaded the sound.
  129. 'Now, we can play it.
  130. 'This function takes several arguments.
  131.  
  132. 'BufLen& represents how much of the sound to play.
  133. 'It represents the number of one-byte samples to scan per period.
  134. '  Set it to SoundSize& to play the whole sound.
  135. '  Set it to less than SoundSize& to hear only the first
  136. '    part of the sound.
  137. BufLen& = SoundSize&   
  138. PRINT "The BufLen& is"; BufLen&
  139.  
  140. 'Reps& is a counter, telling how many times to repeat the sound.
  141. 'Zero means forever, 1 means once.
  142. Reps& = 1
  143. PRINT "The sound will repeat";
  144. IF Reps& = 0 THEN 
  145.   PRINT "forever."
  146. ELSE
  147.   PRINT Reps&; "times."
  148. END IF
  149.  
  150. 'Period& can be found by dividing the magic number
  151. '3579545& by the recording rate.  This should be a minimum of 124.
  152. Period& = INT(3579545& / RecRate&)
  153. PRINT "The Period is"; Period&
  154.  
  155. 'The last argument is the volume, from 0 to 64
  156. Vol& = 64
  157. PRINT "The Volume is"; Vol&
  158.  
  159.  
  160. PlayIt:
  161.  
  162. Result& = FSPlaySound&(MemTry&,BufLen&,Reps&,Period&,Vol&)
  163.  
  164. PRINT "Play this sound again?  Enter Y or N.  > ";
  165. INPUT Again$
  166.  
  167. 'Only stop a sound once!
  168. GOSUB StopIt
  169. IF Again$ = "Y" OR Again$ = "y" THEN
  170.   GOTO PlayIt
  171. END IF
  172.  
  173.  
  174. PRINT "Play another sound?  Enter Y or N.  > ";
  175. INPUT Again$
  176. IF Again$ = "Y" OR Again$ = "y" THEN
  177.   GOTO FileEntry
  178. END IF
  179.  
  180. 'Otherwise free its memory, and stop the program.
  181. GOSUB FreeUpMem
  182. GOTO EndOfIt
  183.  
  184.  
  185. FreeUpMem:
  186.  
  187. PRINT "Freeing the memory used by the sound."
  188.  
  189. 'Since we do not need this memory any more, we should
  190. 'return it to the operating system with the FreeMem function.
  191. 'Be sure not to free memory that is free already.
  192. IF MemTry& = 0 THEN GOTO ForgetIt
  193.  
  194. 'FreeMem() takes two arguments, a pointer to the memory - MemTry&
  195. 'and the size of the memory - MemSize&
  196. CALL FreeMem(MemTry&,MemSize&)
  197.  
  198. ForgetIt:
  199. RETURN
  200.  
  201.  
  202. 'Another function can stop the sound before it finishes.
  203. 'Its only argument is the value returned from FSPlaySound&
  204. 'THIS ROUTINE SHOULD BE CALLED ONLY ONCE PER CALL OF FSPlaySound !!!
  205.  
  206. StopIt:
  207.  
  208. PRINT "Halting the sound."
  209.  
  210. HaltIt& = FSStopSound&(Result&)
  211. RETURN
  212.  
  213.  
  214. 'The end of the program.
  215. EndOfIt:
  216.  
  217. PRINT "End of program."
  218.  
  219. LIBRARY CLOSE
  220.  
  221. PRINT "Library closed."
  222.  
  223. END
  224.  
  225.